<layout name="all@index/layout" />
<script type="text/javascript">
    $(function(){
        var status=$('#status');
        status.combobox({
            url:'{$ROOT}/all/option/studentstatus?only=0',
            valueField:'status',
            textField:'name',
            panelHeight:'auto'
        });
        status.combobox('select', 'A');
        var school=$('#school');
        school.combobox({
            url:'{$ROOT}/all/option/school?only=0&active=1',
            valueField:'school',
            textField:'name'
        });
        var score=$('#score');
        score.combobox({
            url:'{$ROOT}/all/option/scorestatus?only=0',
            valueField:'name',
            textField:'value',
            panelHeight:'auto'
        });
        score.combobox('select', '0');
        $('#dg').datagrid({
            title:'',idField:'studentno', striped:'true',pagination:'true',rownumbers:true,pageSize:20,
            url:'{$ROOT}/score/guidevac/query',
            queryParams: {studentno:''},
            toolbar:'#toolbar',pageList:[20,50,100,200],
            columns:[[
                {field: 'checkbox', title: '选中', checkbox: true},
                {field:'studentno',title:'学号',width:90,align:'center',sortable:true},
                {field:'studentname',title:'姓名',width:70,align:'center',sortable:true},
                {field:'sexname',title:'性别',width:40,align:'center',sortable:true},
                {field:'classno',title:'班号',width:60,align:'center',sortable:true},
                {field:'classname',title:'班级',width:160,align:'center',sortable:true},
                {field:'schoolname',title:'学院',width:130,align:'center',sortable:true},
                {field:'statusname',title:'学籍状态',width:70,align:'center',sortable:true},
                {field:'score',title:'成绩',width:70,align:'center',sortable:true},
                {field:'teachername',title:'提交人',width:70,align:'center',sortable:true},
                {field:'date',title:'时间',width:130,align:'center',sortable:true}
            ]],
            //标题行右键菜单
            onHeaderContextMenu: function(e, field){
                e.preventDefault();
                if (!cmenu_obj.cmenu)//没有的话创建一个
                    $('#dg').datagrid('createColumnMenu',cmenu_obj);
                cmenu_obj.cmenu.menu('show', {
                    left:e.pageX,
                    top:e.pageY
                });
            },
            //数据行上右键菜单
            onRowContextMenu:function(e,rowindex,row){
                e.preventDefault();
                var tt=$('#dg');
                tt.datagrid('selectRow',rowindex);
                $('#menu').menu('show',{
                    left: e.pageX,
                    top: e.pageY
                });
            }
        });
        $("#search").click(function() {
            var tt=$('#dg');
            tt.datagrid('loadData',{total:0,rows:[]});
            tt.datagrid('clearSelections');
            tt.datagrid('clearChecked');
            tt.datagrid('load', {
                studentno: $('#studentno').val(),
                classno: $('#classno').val(),
                studentname:$('#studentname').val(),
                school:$('#school').combobox('getValue'),
                status:$('#status').combobox('getValue'),
                score:$('#score').combobox('getValue')
            });
        });

        $("#pass").click(function() {
            setScore('合格');
        });
        $("#fail").click(function() {
            setScore('不合格');
        });
        function setScore(score){
            var tt=$('#dg');
            var rows=tt.datagrid('getSelections');
            if(checkSchool(rows)){
                var effectRow = {};
                effectRow["score"]=score;
                effectRow["updated"]=$.toJSON(buildDataArray(rows));
                $.post('{$ROOT}/score/guidevac/update', effectRow, function (result) {
                    if (result.status == 1) {
                        tt.datagrid('reload');
                        $.messager.alert('成功',result.info, 'info');

                        tt.datagrid('clearSelections');
                    } else {
                        $.messager.alert('错误', result.info, 'error');
                    }
                }, 'json')
            }
        }
        function checkSchool(rows) {
            var count = rows.length;
            for (var i = 0; i < count; i++) { //教师所在学院不是本学院的，且登录账户不在职能部门不允许修改。
                if (rows[i].school != '{$USERINFO.school}' && '{$USERINFO.manage}' != '1') {
                    $.messager.alert('错误', '你无法修改其他学院学生的成绩（' + rows[i].studentname + '）！', 'error');
                    return false;
                }
            }
            return true;
        }
        //构建数据集
        function buildDataArray(rows){
            var updateRow=[];
            var length=rows.length;
            for (var i = 0; i < length; i++) {
                var updateObj = {}; //插入的单元格
                updateObj.studentno=rows[i].studentno;
                updateRow.push(updateObj);
            }
            return updateRow;
        }
        $('#export_my').click(function() {
            $.fileDownload("{$ROOT}/score/guidevac/export", {
                preparingMessageHtml: "正在导出数据，请稍候...<br/>请勿做其它操作！.",
                dialogOptions: {modal: true, title: '提示'},
                failCallback: function (html, url) {
                    $.messager.alert('错误', html, 'error');
                }
            });
        });


        $('#export_all').click(function() {
            $.fileDownload("{$ROOT}/score/guidevac/exportall?studentno="+encodeURI($('#studentno').val())+"&studentname="+encodeURI($('#studentname').val())
                +"&school="+encodeURI($('#school').combobox('getValue'))+"&status="+encodeURI($('#status').combobox('getValue'))+"&score="+encodeURI($('#score').combobox('getValue'))
                , {
                    preparingMessageHtml: "正在导出数据，请稍候...<br/>请勿做其它操作！.",
                    dialogOptions: {modal: true, title: '提示'},
                    failCallback: function (html, url) {
                        $.messager.alert('错误', html, 'error');
                    }
                });
        });
    });
</script>
<div class="container">
    <div id="toolbar">
        <label for="studentno">学号：</label><input id="studentno" class="easyui-validatebox" size="9" value="%"/>
        <label for="studentname">姓名：</label><input id="studentname" class="easyui-validatebox" size="6" value="%"/>
        <label for="classno">班号：</label><input id="classno" class="easyui-validatebox" size="6" value="%"/>
        <label for="school">学院：</label><input id="school"/>
        <label for="status">学籍状态：</label><input id="status" size="10"/>
        <label for="score">成绩：</label><input id="score" size="10"/>
        <a href="#" class="easyui-linkbutton"  data-options="iconCls:'icon icon-search',plain:'true'" id="search">检索</a> |
        <a href="#" class="easyui-linkbutton"  data-options="iconCls:'icon icon-person',plain:'true'" id="pass">成绩设合格</a>
        <a href="#" class="easyui-linkbutton"  data-options="iconCls:'icon icon-student',plain:'true'" id="fail">成绩设不合格</a>
        <a href="#" class="easyui-splitbutton" data-options="menu:'#mm1',iconCls:'icon icon-excel'">导出数据</a>
    </div>
    <div id="mm1" style="width:100px;">
        <div data-options="iconCls:'icon icon-book',plain:'true'"  id="export_my">我输入的成绩</div>
        <div data-options="iconCls:'icon icon-manage'" id="export_all">全部成绩记录</div>
    </div>
    <table id="dg"></table>
    <div class="space"></div>
    <div class="information">
        <ol>说明：
            <li>成绩“无”表示尚未输入成绩，“已输入”成绩包含合格或者不合格的已经录入过的成绩。</li>
            <li>导出“全部成绩记录”时会包含所有输入过的合格或者不合格成绩。</li>
        </ol>
    </div>
</div>